<?php
namespace app\serverfavorite\logic;

use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-06-12 10:59:57
 * @Description: 收藏分类
 */
class FavoriteCategory extends BaseLogic
{
	protected $name = 'favorite_category';
    protected $config = 'db_config_favorite';
    protected $favoriteCategoryLogic = '\app\serverfavorite\logic\FavoriteCategory';
    protected $favoriteCategoryModel = '\app\serverfavorite\model\FavoriteCategory';
    protected $editAllowField = ['name','profile_pic','description','order'];

    use \app\serveruser\traits\addAppId;
    use \app\servergoods\traits\UpdateCate;
    use \app\servergoods\traits\GetCateTree;

    /*内部获取某一分类的详情*/
    public function dbCateInfo($data)
    {
    	$_where['id'] = $data['id'];
    	$_where['status'] = ['neq',-1];
    	$res = $this->db()->where($_where)->find();
        return $res;
    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map = [])
    {
    	isset($map['id']) && $_where['id'] = $map['id'];
    	isset($map['pid']) && $_where['pid'] = $map['pid'];
        if(isset($map['app_id'])){
            $isManageAppId = $this->_isManageAppId($map['app_id']);
            !$isManageAppId && $_where['app_id'] = $map['app_id'];
        }
    	$_where['status'] = ['neq',-1];
    	return $this->db()->where($_where)->select();
    }
    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
    public function dbAllCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        $_where['status'] = isset($map['status']) ? $map['status'] : ['neq',-1];

        if(isset($map['app_id'])){   //若卖家查看商品详情，则不传入app_id
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $map['app_id'] ? true : '`app_id`=' . $map['app_id'] . ' OR FIND_IN_SET(' . $map['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }
        return $this->db()
                    ->where($_where)
                    ->where($_whereOr)
                    ->select();
    }

    /*编辑收藏分类*/
    public function editCate($data): array 
    {
    	return $this->_updateCate($this->favoriteCategoryLogic, $this->favoriteCategoryModel, $data);
    }

    /*添加收藏分类*/ 
    public function addCate($data): array 
    {
        try{
            $res = $this->model($this->favoriteCategoryModel)->save($data);
        }catch(\Exception $e){
            return server_err(null, '数据库错误' . $e->getMessage() );
        }
    	return auto_return($res, '添加分类成功','添加分类失败');
    }

    /*获取某个分类的详情*/
    public function oneCateInfo($data): array 
    {
        isset($data['id']) && $_where['id'] = $data['id'];
    	isset($data['action']) && $_where['action'] = $data['action'];
    	$_where['status'] = ['neq',-1];
        //$_where = $this->_addAppId($_where, $data['app_id']);
        //app_id的筛选
        if(isset($data['app_id'])){   //若卖家查看商品详情，则不传入app_id
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }
    	$res = $this->model($this->favoriteCategoryModel)->where($_where)->where($_whereOr)->find();
    	return auto_return($res);
    }

    /*获取树状分类列表*/
    public function cateList($data): array 
    {
        return $this->_getCateTree($data, $this->favoriteCategoryLogic, 'dbAllCateList');
    }

    /*删除一个或多个收藏分类*/
    public function batchDelCate($data): array 
    {
    	is_numeric($data['id']) && $data['id'] = [$data['id']];
    	$map['pid'] = ['in',$data['id']];

        $list = $this->dbCateList($map);
        if(count($list) > 0){
            return auto_failure(null,'失败，存在子类【' . $list[0]['name'] . '】');
        }
        $_where['id'] = ['in',$data['id']];
        $_where = $this->_addAppId($_where, $data['app_id']);
        $update['status'] = -1;
        $res = $this->db()
                    ->where($_where)
                    ->update($update);
        return auto_return(1,'删除成功','删除失败');
    }

    /*启用/禁用 一个/多个 分类  传参： id数组 */
    public function batchChangeStatus($data): array
    {
        $list = $this->dbCateList();
    	$saveData['status'] = $data['status'];

    	is_numeric($data['id']) && $data['id'] = [$data['id']];
        foreach ($data['id'] as $k => $v) {
            $childId = child_ids($list, $v, 'id', 'pid', 'id');  //使用static数组 获取该分类下的所有子类
        }
        $data['id'] = array_merge($childId, $data['id']);
        $_where['id'] = ['in',$data['id']];
        $_where = $this->_addAppId($_where, $data['app_id']);
        
    	$res = $this->model($this->favoriteCategoryModel)->save($saveData, $_where);
        return auto_return($res);
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array 
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }
 



}